<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '角色添加' : '角色编辑'"
      :visible.sync="info.isshow"
      @closed="closed"
    >
      {{ user }}
      <el-form :model="user">
        <el-form-item label="角色名称" label-width="120px">
          <el-input v-model="user.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色权限" label-width="120px">
          <el-tree
            :data="treedata"
            :props="{ children: 'children', label: 'title' }"
            show-checkbox
            node-key="id"
            @check="check"
            ref="tree"
          ></el-tree>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add" v-if="this.info.isAdd"
          >添 加</el-button
        >
        <el-button type="primary" @click="update" v-else>修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { menulist, roleadd, roleedit, roleinfo } from '../../../http/api';
import { successAlert } from "../../../utils/alert";
export default {
  props: ["info"],
  data() {
    return {
      user: {
        rolename: "",
        menus: [],
        status: 1,
      },
      treedata: [],
    };
  },
  computed: {
    ...mapGetters({
      userInfo:"userInfo"
    }),
  },
  methods: {
    ...mapActions({
      changeUserInfo:"changeUserInfo"
    }),
    
    cancel(){
        this.info.isshow=false
    },
    add(){
        this.user.menus=JSON.stringify(this.user.menus);
        roleadd(this.user).then(res=>{
            if(res.data.code==200){
                successAlert(res.data.msg);
                this.info.isshow=false;
                this.$emit("init");
                this.empty()
            }
        })
    },
    update(){
      this.user.menus=JSON.stringify(this.user.menus);
        roleedit(this.user).then(res=>{
          if(res.data.code==200){
            successAlert(res.data.msg);
            //判断当前修改的user.id是不是当前登录用户的roleid
            //如果是就自动退出，如果不是，什么都不做
            if(this.user.id==this.userInfo.roleid){
              this.changeUserInfo({});
              this.$router.replace("/login");
              return;
            }
                this.info.isshow=false;
                this.$emit("init");
                this.empty()
          }
        })
    },
    check(){
        this.user.menus=[...(this.$refs.tree.getCheckedKeys())];
    },
    empty(){
        this.user={
            rolename: "",
            menus: [],
            status: 1,
        };
        this.$refs.tree.setCheckedKeys([]);
    },
    getOne(id){
      roleinfo({id}).then(res=>{
        if(res.data.code==200){
          // res.data.list.menus=JSON.parse(res.data.list.menus);
          this.user = res.data.list;
          this.user.id=id;
          this.$refs.tree.setCheckedKeys(JSON.parse(res.data.list.menus));
        }
      })
    },

    //如果是编辑窗口取消，清空数据，添加窗口的取消，不需要清空
    closed(){
      if(!this.info.isAdd){
        this.empty()
      }
    },
  },
  mounted(){
      menulist({istree:true}).then(res=>{
          this.treedata=res.data.list;
      })
  }
};
</script>

<style>
</style>